﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Netfraction
{
    /// <summary>
    /// Interaction logic for ChatWindow.xaml
    /// </summary>
    public partial class ChatWindow : Window
    {
        private FlowDocument chatDoc;

        private delegate void UpdatePrivateChatDelegate(Netfraction.Identity user, string text);
        private delegate void UpdateChatDocDelegate();

        private Netfraction.RemoteMachine.Hub mHub;
        private Netfraction.Identity localIdentity;
        private Netfraction.Identity remoteIdentity;

        public ChatWindow(Netfraction.RemoteMachine.Hub hub, Netfraction.Identity localIdentity, Netfraction.Identity remoteIdentity, string message)
        {
            InitializeComponent();

            this.mHub = hub;
            this.localIdentity = localIdentity;
            this.remoteIdentity = remoteIdentity;
            this.Title = remoteIdentity.Nickname;
            this.Closed += new EventHandler(ChatWindow_Closed);

            chatDoc = new FlowDocument();
//            chatDoc.AddHandler(Hyperlink.RequestNavigateEvent, new System.Windows.Navigation.RequestNavigateEventHandler(chatDoc_LinkClicked));
            //chatDoc.AddHandler(Hyperlink.ClickEvent, new System.Windows.RoutedEventHandler(ChatDocument.chatDoc_LinkClicked));
            chatDoc.SetValue(Paragraph.LineHeightProperty, 1.0);

            messageTextBox.KeyDown += new KeyEventHandler(messageTextBox_KeyDown);

            if (message != null)
            {
                DisplayMessage(remoteIdentity, message);
            }

            messageTextBox.Focus();
        }

        public static void chatDoc_LinkClicked(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Console.WriteLine(sender);
            /*Hyperlink senderLink = sender as Hyperlink;
            if (senderLink != null)
            {
                try
                {
                    if (senderLink.NavigateUri.IsWellFormedOriginalString())
                    {
                        System.Diagnostics.Process.Start(senderLink.NavigateUri.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }*/
        }

        public ChatWindow(Netfraction.RemoteMachine.Hub hub, Netfraction.Identity localIdentity, Netfraction.Identity remoteIdentity)
            : this(hub, localIdentity, remoteIdentity, null)
        {
            
        }

        void ChatWindow_Closed(object sender, EventArgs e)
        {
            ChatWindowManager.ChatWindowClosing(remoteIdentity);
        }

        public void DisplayMessage(Netfraction.Identity user, string message)
        {
            try
            {
                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new UpdatePrivateChatDelegate(UpdatePrivateChat), user, message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.TargetSite.Name);
                Console.WriteLine(e.Message);
            }
        }

        public void UpdatePrivateChat(Netfraction.Identity user, string text)
        {
            chatDoc.Blocks.Add(ChatDocument.CreateChatDocument(user, text));

            this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                    new UpdateChatDocDelegate(UpdateChatDoc));
        }

        public void UpdateChatDoc()
        {
            try
            {
                chatTextBox.Document = chatDoc;
                chatTextBox.ScrollToEnd();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }

        void messageTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                SendMessage(messageTextBox.Document);
                messageTextBox.Document = new FlowDocument();
            }
        }

        private void SendMessage(FlowDocument message)
        {
            TextRange tr = new TextRange(message.ContentStart, message.ContentEnd);
            string text = tr.Text.Trim();
            Netfraction.ClientInterface.Chat.SendPrivateMessage(mHub, remoteIdentity, text);
        }

        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            SendMessage(messageTextBox.Document);
        }
    }
}
